package com.chaodev.base.controller;

import com.chaodev.base.base.RedisDao;
import com.chaodev.base.dto.ResourceListDto;
import com.chaodev.base.model.ResourceList;
import com.chaodev.base.model.Result;
import com.chaodev.base.model.ResultEnum;
import com.chaodev.base.model.UserInfo;
import com.chaodev.base.service.ILogService;
import com.chaodev.base.service.ResourceListService;
import com.chaodev.base.utils.JwtUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/resourceList")
public class ResourceListController {

    @Autowired
    private RedisDao redisDao;

    @Autowired
    private ILogService logService;

    @Autowired
    private ResourceListService resourceListService;

    @PostMapping(value = "/saveResourceList")
    @ResponseBody
    @ApiOperation(value = "保存三类清单")
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result saveResourceList(@RequestBody ResourceList resourceList, HttpServletRequest request, HttpServletResponse response) {
        Result result = new Result();
        Result logResult = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = new UserInfo();
        try {
            if (redisDao.existsKey(token)) {
                userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
                resourceList.setCUserCode(userInfo.getUserId());
                resourceList.setCUserName(userInfo.getUserName());
                resourceList.setOrgCode(userInfo.getDepartId());
                resourceList.setOrgName(userInfo.getDepartName());

                String startTime = resourceList.getStartTime().trim()
                        .replace("-","").replace(":","").replace(" ","");
                String endTime = resourceList.getEndTime().trim()
                        .replace("-","").replace(":","").replace(" ","");
                if (Long.valueOf(startTime) >= Long.valueOf(endTime)){
                    throw new Exception("开始时间必须小于结束时间。");
                }
                if (resourceListService.saveResourceList(resourceList)) {
                    result.setCode(ResultEnum.SUCCESS.getCode());
                    result.setMsg("操作成功");
                } else {
                    result.setCode(-1);
                    result.setMsg("操作失败");
                }
            } else {
                result.setCode(-1);
                result.setMsg("登录超时");
            }
        } catch (Exception exception) {
            result.setCode(-1);
            result.setMsg("发生错误:" + exception.getMessage());
        } finally {
            BeanUtils.copyProperties(result, logResult);
            logResult.setData(null);
            String resultCode = logResult.getCode().equals(ResultEnum.SUCCESS.getCode()) ? "SUCCESS" : "ERROR";
            logService.logAdd(resourceList.toString(), "保存三类清单", resultCode, logResult.toString(), userInfo, request);
            return result;
        }
    }

    @PostMapping(value = "/deleteResourceList")
    @ResponseBody
    @ApiOperation(value = "删除三类清单")
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result deleteResourceList(@RequestBody @ApiParam("id") ResourceList resourceList, HttpServletRequest request, HttpServletResponse response) {
        Result result = new Result();
        Result logResult = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = new UserInfo();
        try {
            if (redisDao.existsKey(token)) {
                userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
                if (resourceListService.deleteResourceList(resourceList)) {
                    result.setCode(ResultEnum.SUCCESS.getCode());
                    result.setMsg("操作成功");
                } else {
                    result.setCode(-1);
                    result.setMsg("操作失败");
                }
            } else {
                result.setCode(-1);
                result.setMsg("登录超时");
            }
        } catch (Exception exception) {
            result.setCode(-1);
            result.setMsg("发生错误:" + exception.getMessage());
        } finally {
            BeanUtils.copyProperties(result, logResult);
            logResult.setData(null);
            String resultCode = logResult.getCode().equals(ResultEnum.SUCCESS.getCode()) ? "SUCCESS" : "ERROR";
            logService.logAdd(resourceList.toString(), "删除三类清单", resultCode, logResult.toString(), userInfo, request);
            return result;
        }
    }

    @PostMapping(value = "/getResourceListPage")
    @ApiOperation(value = "查询三类清单分页")
    @ResponseBody
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result getResourceListPage(@RequestBody ResourceListDto resourceListDto, HttpServletRequest request, HttpServletResponse response){
        Result result = new Result();
        String token = request.getHeader("token");
        Result logResult = new Result();
        UserInfo userInfo = new UserInfo();
        try {
            if (redisDao.existsKey(token)) {
                userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
                if (resourceListDto.getIsCreateLimit() != null && resourceListDto.getIsCreateLimit() == 1){
                    resourceListDto.setCUserCode(userInfo.getUserId());
                }
                result.setCode(ResultEnum.SUCCESS.getCode());
                result.setData(resourceListService.getResourceListPage(resourceListDto));
                result.setMsg("数据获取成功");
            } else {
                result.setCode(-1);
                result.setMsg("登录超时");
            }
        } catch (Exception exception) {
            result.setCode(-1);
            result.setMsg("发生错误:" + exception.getMessage());
        } finally {
            BeanUtils.copyProperties(result, logResult);
            logResult.setData(null);
            String resultCode = logResult.getCode().equals(ResultEnum.SUCCESS.getCode()) ? "SUCCESS" : "ERROR";
            logService.logAdd(resourceListDto.toString(), "查询三类清单分页", resultCode, logResult.toString(), userInfo, request);
            return result;
        }
    }

    @PostMapping(value = "/getResourceListById")
    @ApiOperation(value = "id查询单条三类清单")
    @ResponseBody
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result getResourceListById(@RequestBody ResourceListDto resourceListDto, HttpServletRequest request, HttpServletResponse response){
        Result result = new Result();
        String token = request.getHeader("token");
        Result logResult = new Result();
        UserInfo userInfo = new UserInfo();
        try {
            if (redisDao.existsKey(token)) {
                userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
                result.setCode(ResultEnum.SUCCESS.getCode());
                result.setData(resourceListService.getResourceListById(resourceListDto));
                result.setMsg("数据获取成功");
            } else {
                result.setCode(-1);
                result.setMsg("登录超时");
            }
        } catch (Exception exception) {
            result.setCode(-1);
            result.setMsg("发生错误:" + exception.getMessage());
        } finally {
            BeanUtils.copyProperties(result, logResult);
            logResult.setData(null);
            String resultCode = logResult.getCode().equals(ResultEnum.SUCCESS.getCode()) ? "SUCCESS" : "ERROR";
            logService.logAdd(resourceListDto.toString(), "id查询单条三类清单", resultCode, logResult.toString(), userInfo, request);
            return result;
        }
    }

}
